<template>
    <div class="app">
        <div class="app-route">
             <span class="app-route__name">{{checkedRouter.name}}</span>
              <el-popover trigger="click"
                          placement="bottom-start"
                          v-model="showDropdown"
                          :visible-arrow="false"
                          popper-class="app-route__popover">
                <p class="app-route__link"
                   v-for="link in routerData"
                   :key="link.name"
                   @click="routerJump(link)">{{link.name}}</p>
                <app-icon slot="reference"
                          iconName="vcloud-arrow-down"
                          className="org-info__icon cursor-pointer hover-blue"/>
                <!-- <div slot="reference" class="cursor-pointer">
                    <i class="el-icon-arrow-down" />
                </div> -->
            </el-popover>
            
        </div>
        <!-- <button @click="openInstallDialog('5957225183275663419',1)">测试</button>  -->
        <router-view ref="page" />
        <!-- 应用导入提示框 -->
        <UploadDialog  ref="UploadDialog" @openAppInstall="res=>openInstallDialog(res.fileId,res.type,res.data)"/>
        <!-- 应用导出提示框 -->
        <DownloadDialog ref="DownloadDialog"/>
    </div>
</template>

<script>
import LocalUploadMixins from "@/mixins/localUpload.js"
import AppFunctionMixins from "@/mixins/appFunction.js"
import UploadDialog from "@/components/uploadDialog.vue"
import DownloadDialog from "./components/downloadDialog.vue"
import RefreshJsessionIdMixins from "@/mixins/refreshJsessionId"
import {
    createNamespacedHelpers
} from "vuex";
const {
    mapState,
    mapMutations
} = createNamespacedHelpers("user");

export default {
    name: "app",
    mixins: [
        LocalUploadMixins,
        AppFunctionMixins,
        RefreshJsessionIdMixins
    ],
    components: {
        UploadDialog,
        DownloadDialog
    },
    computed: {
        ...mapState([
            "ssoV5Url",
        ]),
        checkedRouter() {
            return this.routerData.find(item => this.$route.path === item.routerPath);
        }
    },
    data: () => ({
        routerData: [
            {
                name: "我的应用",
                routerPath: "/main/app/myApp",
                checked: true
            },
            {
                name: "单位所有应用",
                routerPath: "/main/app/orgApp",
                checked: false
            },
        ],
        showDropdown: false,
    }),
    created() {},
    mounted() {},
    watch: {
    },
    methods: {

        //打开本地导入提示框
        openLocalDialog() {
            let data = {
                userInfo:this.userInfo, //用户信息
                localRefresh:false,  //本地上传为false
                fileType:["syz"]    //支持上传的文件类型
            }
            this.$refs.UploadDialog.open(data);
        },
        //打开本地更新提示框
        openLocalRefreshDialog() {
            let data = {
                userInfo:this.userInfo,
                localRefresh:true, //本地更新为true
                fileType:["syz"]  //支持上传的文件类型
            }
            this.$refs.UploadDialog.open(data);
        },
        //打开导出提示框
        openDownloadDialog(bizConfigId,title){
            let data = {
                userInfo:this.userInfo,
                bizConfigId, //应用包id
                title    //应用包名
            }
            this.$refs.DownloadDialog.open(data);
        },
        
        routerJump(link) {
            this.routerData.forEach(item => {
                item.checked = item.routerPath === link.routerPath;
            })
            if(this.$route.path!==link.routerPath) this.$router.push(link.routerPath)
            this.showDropdown = false
        },
        openInstallDialog:function(fileId,type,data){
            let that = this;
            //type 1  创建新应用   2 本地更新应用   3 更新在线应用
            let installParams = {
                fileId:fileId,
                resetId:false,
                setupType:'local',
                source:'',
                title: "应用安装向导",
                upgrade: false,
                upgradeAppId: "",
                url: "",
                version: ""
            };
            if(type&&type != 1&&data){
                installParams.title = '应用升级向导';
                installParams.upgrade = true;
                installParams.setupType = "local";
                if(data.source){
                    installParams.source = data.source;
                }
                installParams.upgradeAppId = data.id;
            }else if(type == 3&&data){
                installParams.setupType = "online";
                installParams.url = data.url;
            }
            this.getDdV5SsoToken();
            let dialog = $.dialog({
                id: 'bizSetupDialog',
                /*url: _ctxPath +
                    '/common/cap4/bizconfig/managementCenter/index.html?${ctp:resSuffix()}' + CsrfGuard
                    .getUrlSurffix(),*/
                url: this.ssoV5Url +  encodeURIComponent('/common/cap4/bizconfig/managementCenter/index.html'),
                width: 880,
                height: 510,
                title: installParams.title,
                transParams: installParams,
                checkMax: true,
                closeParam: {
                    'show': false,
                    autoClose: false,
                    handler: function () {
                        var res = dialog.getReturnValue({
                            type: "close"
                        });
                        installParams.bizConfigId = res.bizConfigId;
                        callBackendMethod("capAppSetupManager", "cancelSetup", installParams);
                    }
                },
                buttons: [{
                    id: 'next',
                    text: "下一步",
                    handler: function () {
                        dialog.startLoading();
                        var resut = dialog.getReturnValue({
                            type: "next"
                        });
                        if (resut) {
                            dialog.close();
                        }
                    }
                }, {
                    id: 'cancel',
                    text: '取消',
                    handler: function () {
                        top.$.confirm({
                            'msg': '取消安装或关闭安装窗口会清空已配置数据，确定吗？',
                            ok_fn: function () {
                                dialog.endLoading();
                                var res = dialog.getReturnValue({
                                    type: "close"
                                });
                                installParams.bizConfigId = res.bizConfigId;
                                callBackendMethod("capAppSetupManager",
                                    "cancelSetup", installParams);
                                dialog.close();
                            },
                            cancel_fn: function () {

                            }
                        });
                    }
                }, {
                    id: 'OK',
                    text: "完成",
                    hide: true,
                    handler: function () {
                        var result = dialog.getReturnValue({
                            type: "complate"
                        });
                        //capTop.refreshCap4Menu();
                        if (result && result.hasBaseData) { //点击完成时如果有基础数据，则弹出基础数据导入窗口
                            dialog.endLoading();
                            dialog.close();
                            that.openBaseDataImportDialog(result,that);
                        } else {
                            dialog.endLoading();
                            dialog.close();
                            that.updateAppList(that);
                        }
                    }
                }]
            });
        },
        openBaseDataImportDialog:(data,that)=>{
            this.getDdV5SsoToken();
            var  dialog = top.$.dialog({
                id: 'basicDataImport',
                url:this.ssoV5Url + encodeURIComponent(`/common/cap4/template/base/cap4-batch/package.html??V=V8_0_2020-03-23_1976539876`),
                width: this.findParent().innerWidth - 40,
                height: this.findParent().innerHeight - 105,
                title: "基础数据安装向导 - 数据记录导入",
                transParams: {bizConfigId:data.bizConfigId},
                checkMax: true,
                buttons: [{
                    id: 'next',
                    text: "下一步",
                    handler: function () {
                        dialog.startLoading();
                        var result = dialog.getReturnValue({type: "next"});
                        //下一步，如果OK方法返回true就关闭，否则不做任何处理
                        if (result===true) {
                            dialog.close();
                        }
                    }
                }, {
                    id: 'reimport',
                    text: "重新导入",
                    hide:true,
                    handler: function () {
                        var result = dialog.getReturnValue({type: "reimport"});
                    }
                }, {
                    id: 'close',
                    text: '关闭',
                    handler: function () {
                        var promis = dialog.getReturnValue({type: "close"});
                        if(promis){
                            promis.then(function(result){
                                if(result===true){
                                    dialog.close();
                                    that.updateAppList(that);
                                    // window.location.reload();
                                }
                            });
                        }
                    }
                }]
            });
        },
        findParent:()=>{
            let current = window;
            if (current.parent) {
                current = current.parent;
            }
            return current;
        },
        updateAppList:function(that){
            that.$refs.page.$refs.appList.getData(1);
        }
    }
};
</script>

<style lang="scss" src="../../assets/styles/app/index.scss" scoped></style>
<style lang="scss">
.el-popper.app-route__popover {
    padding: 0;
    border-radius: 2px;
    .app-route__link {
        line-height: 32px;
        color: #000;
        padding: 0 12px;
        font-size: 12px;
        font-weight: 600;
        &:hover {
            color: #fff;
            background-color: #336CFB;
        }
    }
}
</style>
